package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import com.alibaba.druid.sql.visitor.functions.If;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private StrategyMapper strategyMapper;

    @Autowired
    private StrategyTagMapper strategyTagMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public void changeState(Long id, int state) {
        strategyDetailMapper.updateState(id, state);
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<StrategyDetail> getDetailTop3(Long id) {
        return strategyDetailMapper.selectDetailTop3(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail detail, String tags) {

        //目的地处理,使用大攻略绑定目的地
        //获取此明细攻略
        Strategy strategy = strategyService.get(detail.getStrategy().getId());
        //通过攻略来获取目的地
        Destination dest = strategy.getDest();
        //把目的地绑定在明细上
        detail.setDest(dest);

        //摘要detail
        //截取文章内容前200字
        //如果不满200字就放完整内容上去
        String content = detail.getStrategyContent().getContent();
        //判断字数
        if (content.length()>200){
            detail.setSummary(content.substring(0,200));
        }else {
            detail.setSummary(content);
        }

        //是否国外
        //获取吐司,通过目的地id
        List<Destination> toasts = destinationService.getToasts(dest.getId());
        //获取索引为0的父级,即国家级
        Destination country = toasts.get(0);
        if (country.getId() == 1){
            detail.setIsabroad(false);
        }else {
            detail.setIsabroad(true);
        }

        //创建时间
        if (detail.getId() == null){
            detail.setCreateTime(new Date());
            strategyDetailMapper.insert(detail);
            //内容
            //根据明细来获取攻略内容
            StrategyContent strategyContent = detail.getStrategyContent();
            strategyContent.setId(detail.getId());
            strategyContentMapper.insert(strategyContent);
        }else {
            strategyDetailMapper.updateByPrimaryKey(detail);
            StrategyContent strategyContent = detail.getStrategyContent();
            strategyContent.setId(detail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }

        //打破关系先
        strategyDetailMapper.deleteRelation(detail.getId());
        //切tag标签name操作
        if (StringUtils.hasLength(tags)){
            //有长度按照,切割
            String[] split = tags.split(",");
            for (String s : split) {
                StrategyTag strategyTag = new StrategyTag();
                //标签名设置到tag里面
                strategyTag.setName(s);
                //设置状态
                strategyTag.setState(StrategyTag.STATE_NORMAL);
                strategyTagService.saveOrUpdate(strategyTag);

                //维护关系
                strategyDetailMapper.insertRelation(detail.getId(),strategyTag.getId());
            }
        }
    }

    @Override
    public List<StrategyDetail> getViewNumTop3(Long id) {
        return strategyDetailMapper.selectViewNumTop3(id);
    }

    @Override
    public void delete(Long id) {
        strategyDetailMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateReplynum(Long id, int replynum) {
        strategyDetailMapper.updateReplynum(id,replynum);
    }

    @Override
    public void updateStatisVO(StrategyStatisPersistenceVO vo) {
        strategyDetailMapper.updateStatisVO(vo);
    }
}
